import pandas
import numpy
from matplotlib import pyplot
import seaborn
# !cp SimHei.ttf /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/mpl-data/fonts/ttf
# pyplot.rcParams['font.sans-serif']='SimHei'
# pyplot.rcParams['axes.unicode_minus']=False
data=pandas.read_csv('某地区房屋销售数据.csv',sep=',',encoding='gbk')
print(data.ndim)
print(data.columns)
print(data.shape)
print(data.describe())
#直接查询
print(data[data['房屋类型']=='unit'])
#通过loc查询
print(data.loc[data['房屋类型']=='unit',:])
#通过iloc查询
data.iloc[(data['房屋类型']=='unit').values,:]
#将时间字符串转化为datetime对象
data['房屋出售时间']=pandas.to_datetime(data['房屋出售时间'])
#获取年份
print(data['房屋出售时间'].apply(lambda x:x.year))
#计算房屋价格的均值、最大值、最小值和众数
print(data['房屋价格'].mean())
print(data['房屋价格'].min())
print(data['房屋价格'].max())
print(data['房屋价格'].mode())
#获取房屋价格的分位数
print(data['房屋价格'].quantile(q=[0,0.25,0.75,1]))
#获取房屋价格的各种统计量
print(data['房屋价格'].describe())
data['new_postcode']=data['地区邮编'].apply(lambda x:x//100)
groups=data.groupby(by='new_postcode')
print(groups.agg({'房屋价格':['mean','sum'],'配套房间数':['mean','sum']}))
data['mean']=groups['房屋价格'].transform('mean')
data
mean=groups['房屋价格'].agg('mean')
label=mean.index
#绘制条形图
pyplot.bar(x=[0,1],height=mean)
pyplot.xticks([0,1],label)
#绘制散点图
pyplot.figure()
seaborn.stripplot(data=data,x='new_postcode',y='房屋价格',hue='房屋类型',jitter=True)
#绘制饼状图
pyplot.figure(figsize=(5,10))
pyplot.subplot(3,1,1)
pyplot.pie(data['房屋类型'].value_counts(),labels=data['房屋类型'].value_counts().index,explode=[0.05,0.05],autopct='%.2f%%')
pyplot.title('all')
pyplot.subplot(3,1,2)
pyplot.pie(data[data['new_postcode']==26]['房屋类型'].value_counts(),labels=data['房屋类型'].value_counts().index,explode=[0.05,0.05],autopct='%.2f%%')
pyplot.title('26')
pyplot.subplot(3,1,3)
pyplot.pie(data[data['new_postcode']==29]['房屋类型'].value_counts(),labels=data['房屋类型'].value_counts().index,explode=[0.05,0.05],autopct='%.2f%%')
pyplot.title('29')
#绘制箱线图
pyplot.figure()
pyplot.boxplot((data['房屋价格'],data[data['new_postcode']==26]['房屋价格'],data[data['new_postcode']==29]['房屋价格']),notch=True,labels=['all','26','29'])
#透视表
print(pandas.pivot_table(data=data,values=['房屋价格','配套房间数'],index='new_postcode',aggfunc='mean'))
print(pandas.pivot_table(data=data,values='房屋价格',index='new_postcode',columns='房屋类型'))
#交叉表与透视表类似，这里不进行操作了
#查看数据中书否有空缺值
print(data.isna().sum())
#查看是否有重复的项
print(data.duplicated())#无重复项，无需进行去重
data['year']=data['房屋出售时间'].apply(lambda x:x.year)
#按照年份进行分组分析
groups=data.groupby(by='year',axis=0)
print(groups.agg({'房屋价格':['mean','sum'],'配套房间数':['mean','sum']}))
#分析房屋随时间的变化
x=list(range(2010,2020))
price_mean=groups['房屋价格'].agg('mean')
price_sum=groups['房屋价格'].agg('sum')
room_mean=groups['配套房间数'].agg('mean')
room_sum=groups['配套房间数'].agg('sum')
pyplot.figure()
pyplot.subplot(2,1,1)
pyplot.plot(x,price_mean,label='mean')
pyplot.xlabel('year')
pyplot.ylabel('price_mean')
pyplot.xticks(x)
pyplot.subplot(2,1,2)
pyplot.plot(x,price_sum,label='sum')
pyplot.xlabel('year')
pyplot.ylabel('price_sum')
pyplot.xticks(x)
pyplot.figure()
pyplot.subplot(2,1,1)
pyplot.plot(x,room_mean,label='mean')
pyplot.xlabel('year')
pyplot.ylabel('room_mean')
pyplot.xticks(x)
pyplot.subplot(2,1,2)
pyplot.plot(x,room_sum,label='sum')
pyplot.xlabel('year')
pyplot.ylabel('room_sum')
pyplot.xticks(x)
#线性拟合
new_data=groups.agg({'房屋价格':'mean'})
new_data['year']=x
seaborn.lmplot(data=new_data,x='year',y='房屋价格')